clear

// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

import dbase using "local\gis_data\003_boundaries\fishnet_pt.dbf", clear

rename POINT_X lon
rename POINT_Y lat
rename *, lower
rename joinid objectid


drop join_count closest target_fid
drop nl_name_1 nl_name_2
drop gid_0-lat

sum objectid

**
** generate dummies from categorical python output
**

** admin 1
**
** replace 0 with . (outside of 6th neighbouring admin)
replace bh3ss=. if bh3ss==0
replace bhbrs=. if bhbrs==0
replace bhbs=. if bhbs==0

* gen nei var less 1 from BH definition (BH==1)
g neibh3ss = bh3ss-1
g neibhbrs = bhbrs-1
g neibhbs = bhbs-1

* drop itself (BH==0)
replace neibh3ss=. if neibh3ss==0
replace neibhbrs=. if neibhbrs==0
replace neibhbs=. if neibhbs==0

* gen dummies
tabulate neibh3ss, generate(neibh3ss)
tabulate neibhbrs, generate(neibhbrs)
tabulate neibhbs, generate(neibhbs)

drop neibh3ss neibhbrs neibhbs

* convert categorical to dummy for BH definitions
replace bh3ss=0 if bh3ss>1
replace bhbrs=0 if bhbrs>1
replace bhbs=0 if bhbs>1

**
** admin 2
**
** replace 0 with . (outside of 6th neighbouring admin)
replace bh3sd=. if bh3sd==0
replace bhbrd=. if bhbrd==0
replace bhbd=. if bhbd==0

* gen nei var less 1 from BH definition (BH==1)
g neibh3sd = bh3sd-1
g neibhbrd = bhbrd-1
g neibhbd = bhbd-1

* drop itself (BH==0)
replace neibh3sd=. if neibh3sd==0
replace neibhbrd=. if neibhbrd==0
replace neibhbd=. if neibhbd==0

* gen dummies
tabulate neibh3sd, generate(neibh3sd)
tabulate neibhbrd, generate(neibhbrd)
tabulate neibhbd, generate(neibhbd)

drop neibh3sd neibhbrd neibhbd

* convert categorical to dummy for BH definitions
replace bh3sd=0 if bh3sd>1
replace bhbrd=0 if bhbrd>1
replace bhbd=0 if bhbd>1

**
** admin 3
**
** replace 0 with . (outside of 6th neighbouring admin)
replace bh3sc=. if bh3sc==0
replace bhbrc=. if bhbrc==0
replace bhbc=. if bhbc==0

* gen nei var less 1 from BH definition (BH==1)
g neibh3sc = bh3sc-1
g neibhbrc = bhbrc-1
g neibhbc = bhbc-1

* drop itself (BH==0)
replace neibh3sc=. if neibh3sc==0
replace neibhbrc=. if neibhbrc==0
replace neibhbc=. if neibhbc==0

* gen dummies
tabulate neibh3sc, generate(neibh3sc)
tabulate neibhbrc, generate(neibhbrc)
tabulate neibhbc, generate(neibhbc)

drop neibh3sc neibhbrc neibhbc

* convert categorical to dummy for BH definitions
replace bh3sc=0 if bh3sc>1
replace bhbrc=0 if bhbrc>1
replace bhbc=0 if bhbc>1


** labels
label variable objectid "Seq id per grid or split grid at country border"

label variable bh3ss "Boko-Haram 3 state - dummy state level"
label variable bhbs "Boko-Haram borno - dummy state level"
label variable bhbrs "Boko-Haram between rivers - dummy state level"

label variable bh3sd "Boko-Haram 3 state - dummy district level"
label variable bhbd "Boko-Haram borno - dummy district level"
label variable bhbrd "Boko-Haram between rivers - dummy district level"

label variable bh3sc "Boko-Haram 3 state - dummy commune level"
label variable bhbc "Boko-Haram borno - dummy commune level"
label variable bhbrc "Boko-Haram between rivers - dummy commune level"

label variable neibh3ss1 "1st neighbouring state of BH 3 states"
label variable neibh3ss2 "2nd neighbouring state of BH 3 states"
label variable neibh3ss3 "3rd neighbouring state of BH 3 states"
label variable neibh3ss4 "4th neighbouring state of BH 3 states"
label variable neibh3ss5 "5th neighbouring state of BH 3 states"

label variable neibh3sd1 "1st neighbouring district of BH 3 states"
label variable neibh3sd2 "2nd neighbouring district of BH 3 states"
label variable neibh3sd3 "3rd neighbouring district of BH 3 states"
label variable neibh3sd4 "4th neighbouring district of BH 3 states"
label variable neibh3sd5 "5th neighbouring district of BH 3 states"

label variable neibh3sc1 "1st neighbouring commune of BH 3 states"
label variable neibh3sc2 "2nd neighbouring commune of BH 3 states"
label variable neibh3sc3 "3rd neighbouring commune of BH 3 states"
label variable neibh3sc4 "4th neighbouring commune of BH 3 states"
label variable neibh3sc5 "5th neighbouring commune of BH 3 states"

label variable neibhbs1 "1st neighbouring state of BH borno"
label variable neibhbs2 "2nd neighbouring state of BH borno"
label variable neibhbs3 "3rd neighbouring state of BH borno"
label variable neibhbs4 "4th neighbouring state of BH borno"
label variable neibhbs5 "5th neighbouring state of BH borno"

label variable neibhbd1 "1st neighbouring district of BH borno"
label variable neibhbd2 "2nd neighbouring district of BH borno"
label variable neibhbd3 "3rd neighbouring district of BH borno"
label variable neibhbd4 "4th neighbouring district of BH borno"
label variable neibhbd5 "5th neighbouring district of BH borno"

label variable neibhbc1 "1st neighbouring commune of BH borno"
label variable neibhbc2 "2nd neighbouring commune of BH borno"
label variable neibhbc3 "3rd neighbouring commune of BH borno"
label variable neibhbc4 "4th neighbouring commune of BH borno"
label variable neibhbc5 "5th neighbouring commune of BH borno"


label variable neibhbrs1 "1st neighbouring state of BH between rivers"
label variable neibhbrs2 "2nd neighbouring state of BH between rivers"
label variable neibhbrs3 "3rd neighbouring state of BH between rivers"
label variable neibhbrs4 "4th neighbouring state of BH between rivers"
label variable neibhbrs5 "5th neighbouring state of BH between rivers"

label variable neibhbrd1 "1st neighbouring district of BH between rivers"
label variable neibhbrd2 "2nd neighbouring district of BH between rivers"
label variable neibhbrd3 "3rd neighbouring district of BH between rivers"
label variable neibhbrd4 "4th neighbouring district of BH between rivers"
label variable neibhbrd5 "5th neighbouring district of BH between rivers"

label variable neibhbrc1 "1st neighbouring commune of BH between rivers"
label variable neibhbrc2 "2nd neighbouring commune of BH between rivers"
label variable neibhbrc3 "3rd neighbouring commune of BH between rivers"
label variable neibhbrc4 "4th neighbouring commune of BH between rivers"
label variable neibhbrc5 "5th neighbouring commune of BH between rivers"

sort objectid

save "proc_data\TREAT.dta", replace
